﻿/**
 * Copyright (c) 2013 Nokia Corporation.
 */

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace Dradis
{
    /// <summary>
    /// Simple page for managing the application settings.
    /// </summary>
    public partial class SettingsPage : PhoneApplicationPage
    {
        private ApplicationSettings _settings = null;

        public SettingsPage()
        {
            InitializeComponent();
            UpdateControls();
        }

        /// <summary>
        /// Updates the controls based on the current settings.
        /// </summary>
        private void UpdateControls()
        {
            _settings = ApplicationSettings.GetCopy();

            AllowLocationCheckBox.IsChecked = _settings.LocationAllowed;

            if (_settings.TwitterChannel != null)
            {
                TwitterChannelTextBox.Text = _settings.TwitterChannel;
            }

            RaiderRadioButton.IsChecked = (_settings.UserObjectType == User.ObjectType.Raider);
            ViperRadioButton.IsChecked = (_settings.UserObjectType == User.ObjectType.Viper);

            if (_settings.TwitterToken == null)
            {
                ClearTokenButton.IsEnabled = false;
            }
        }

        /// <summary>
        /// Registers the events of the controls.
        /// </summary>
        private void RegisterEvents()
        {
            AllowLocationCheckBox.Checked += AllowLocationCheckBox_Checked;
            AllowLocationCheckBox.Unchecked += AllowLocationCheckBox_Unchecked;
            TwitterChannelTextBox.TextChanged += TwitterChannelTextBox_TextChanged;
            RaiderRadioButton.Checked += RaiderRadioButton_Checked;
            ViperRadioButton.Checked += ViperRadioButton_Checked;
            ClearTokenButton.Click += ClearTokenButton_Click;
        }

        void AllowLocationCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            _settings.LocationAllowed = false;
        }

        void AllowLocationCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            _settings.LocationAllowed = true;
        }

        void TwitterChannelTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _settings.TwitterChannel = TwitterChannelTextBox.Text;
        }

        void RaiderRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            _settings.UserObjectType = User.ObjectType.Raider;
        }

        void ViperRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            _settings.UserObjectType = User.ObjectType.Viper;
        }

        void ClearTokenButton_Click(object sender, RoutedEventArgs e)
        {
            ApplicationSettings.GetInstance().TwitterToken = null;
            ApplicationSettings.GetInstance().Save();
            TwitterManager.GetInstance().IsAuthenticated = false;
            ClearTokenButton.IsEnabled = false;
        }

        /// <summary>
        /// If the page is not in proper shape, it will be re-initialized.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (_settings == null)
            {
                UpdateControls();
            }

            RegisterEvents();
        }

        /// <summary>
        /// Discards any changes.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _settings = null;

            // Unregister events
            AllowLocationCheckBox.Checked -= AllowLocationCheckBox_Checked;
            AllowLocationCheckBox.Unchecked -= AllowLocationCheckBox_Unchecked;
            TwitterChannelTextBox.TextChanged -= TwitterChannelTextBox_TextChanged;
            RaiderRadioButton.Checked -= RaiderRadioButton_Checked;
            ViperRadioButton.Checked -= ViperRadioButton_Checked;
            ClearTokenButton.Click -= ClearTokenButton_Click;
        }

        /// <summary>
        /// Saves the settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Apply_Click(object sender, EventArgs e)
        {
            ApplicationSettings globalSettings = ApplicationSettings.GetInstance();
            globalSettings.Set(_settings);

            if (!globalSettings.Save())
            {
                System.Diagnostics.Debug.WriteLine(
                    "SettingsPage::Apply_Click(): Failed to save the settings!");
            }

            globalSettings.NotifySettingsChanged();
            NavigationService.GoBack();
        }
    }
}